module TestGame;

import tango.io.Console;

import derelict.opengl.gl;
import derelict.opengl.gltypes;
import derelict.opengl.glu;
import derelict.sdl.sdl;
import derelict.util.loader;

import dgl.Event;
import dgl.Game;
import dgl.GameTime;
import dgl.Vector2;
import dgl.Vector3;
import dgl.Vector4;
import dgl.Rectangle;
import dgl.Matrix;

import dgl.content.ContentManager;

import dgl.input.Mouse;
import dgl.input.MouseState;
import dgl.input.MouseEventArgs;

import dgl.graphics.Model;
import dgl.graphics.ModelMesh;
import dgl.graphics.VertexData;
import dgl.graphics.Color;
import dgl.graphics.FaceData;
import dgl.graphics.GraphicsDevice;
import dgl.graphics.Texture2D;
import dgl.graphics.TexSeqPlayer;
import dgl.graphics.ShaderProgram;
import dgl.graphics.FragmentShader;
import dgl.graphics.VertexShader;

class TestGame : Game
{
	private GLuint vsID;
	
	private GLuint fsID;
	
	Model model;
	
	ShaderProgram sp;
	
	Texture2D texture;
	
	float rotationZ;
	
	float rotationX;
	
	public this()
	{
		super();
		this.window.clientBounds = Rectangle( 0, 0, 800, 600 );
		this.window.title = "Test GLSL";
		this.cursorDisplay = false;
		this.rotationX = 0;
		this.rotationZ = 0;
	}
	
	protected void initialize()
	{
		GraphicsDevice.matView = Matrix.createLookAt( Vector3( 0, -4, 0 ), Vector3.zero, Vector3.unitZ );
		GraphicsDevice.matProj = Matrix.createPerspectiveFieldOfView( 3.14 / 4, 800f / 600f, 1, 10000 );
		GraphicsDevice.clearColor = Color.cornflowerBlue;
	}
	
	protected void loadContent()
	{
		model = ContentManager.load!(Model)( "Content\\Model\\NewBall.dmo" );
		texture = ContentManager.load!(Texture2D)( "Content\\Texture\\FieldStone.tga" );
		
		auto vs = ContentManager.load!(VertexShader)( "Content\\Effect\\PhongVS.txt" );
		auto fs = ContentManager.load!(FragmentShader)( "Content\\Effect\\PhongFS.txt" );
		sp = new ShaderProgram( vs, fs );
	}
	
	protected void unloadContent()
	{
		
	}
	
	protected void update( GameTime gameTime )
	{
		Uint8 * keys = SDL_GetKeyState( null );
		if( keys[SDLK_w] )
		{
			rotationX -= 0.005f;
		}
		if( keys[SDLK_s] )
		{
			rotationX += 0.005f;
		}
		if( keys[SDLK_a] )
		{
			rotationZ -= 0.005f;
		}
		if( keys[SDLK_d] )
		{
			rotationZ += 0.005f;
		}
	}
	
	protected void draw( GameTime gameTime )
	{
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		glEnable( GL_DEPTH_TEST );
		
		model.matWorld = Matrix.createRotationZ( rotationZ );
		model.matWorld = model.matWorld * Matrix.createRotationX( rotationX );
		
		foreach( container; model.meshContainers )
		{
			container.mesh.shader = sp;
			
			sp.uniforms["fvLightPosition"] = Vector3( -4, 4, 4 );
			sp.uniforms["fvEyePosition"] = Vector3( 0, 4, 4 );
			
			sp.uniforms["fvAmbient"] = Color( 0, 0, 0 ).toVector4();
			sp.uniforms["fvSpecular"] = Color( 255, 255, 255 ).toVector4();
			sp.uniforms["fvDiffuse"] = Color( 255, 255, 255 ).toVector4();
			sp.uniforms["fSpecularPower"] = 16f;
			
			sp.uniforms["baseMap"] = texture;
		}
		
		model.draw( gameTime );
	}
}